<template>
  <div id="swiper">
    <div class="recommendPage">
      <swiper :options="swiperOption" ref="mySwiper" v-if="banner.length > 0">
        <swiper-slide
          v-for="(item, index) in banner"
          :key="index"
          class="slide"
        >
          <a :href="item.link">
            <img :src="item.image" alt="" @load="imageload" />
          </a>
        </swiper-slide>
        <div class="swiper-pagination" slot="pagination"></div>
        <div class="swiper-button-prev" slot="button-prev"></div>
        <div class="swiper-button-next" slot="button-next"></div>
      </swiper>
    </div>
  </div>
</template>
 
<script>
// 引入插件
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/swiper-bundle.css";

export default {
  props: {
    banner: {
      type: Array,
      default() {
        return [];
      },
    },
  },
  components: {
    swiper,
    swiperSlide,
  },

  data() {
    return {
      swiperOption: {
        loop: true,
        autoplay: {
          delay: 3000,
          stopOnLastSlide: false,
          disableOnInteraction: false,
          //修改swiper自己或子元素时，自动初始化swiper
          observer: true, //修改swiper自己或子元素时，自动初始化swiper
          observeParents: true, //修改swiper的父元素时，自动初始化swiper
        },
        // 显示分页
        pagination: {
          el: ".swiper-pagination",
          clickable: true, //允许分页点击跳转
        },
        // 设置点击箭头
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
      },
      isload: false,
    };
  },
  methods: {
    imageload() {
      if (!this.isload) {
        this.$emit("imgload");
        this.isload = true;
      }
    },
  },
  // computed: {
  //   swiper() {
  //     return this.$refs.mySwiper.swiper;
  //   },
  // },
  created() {
    this.$forceUpdate();
  },
  mounted() {
    // current swiper instance
    // 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
    // this.swiper.slideTo(3, 1000, false);
  },
};
</script>
<style scoped>
.recommendPage .swiper-container {
  position: relative;
  width: 100%;
  height: 200px;
}
.recommendPage .swiper-container .swiper-slide {
  width: 100%;
  line-height: 200px;
  color: #000;
  font-size: 16px;
  text-align: center;
}

.recommendPage {
  margin-top: 39px;
}

.slide img {
  width: 100%;
  object-fit: cover;
}
.swiper-wrapper {
  transition-timing-function: linear !important;
}
</style>